// $Id: ptr_multimap.h 9 2008-09-30 03:21:47Z rodlima $

/*
   Copyright (c) 2008, Rodolfo Schulz de Lima 
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.  
   * Neither the name of RodSoft nor the names of its contributors may be used
     to endorse or promote products derived from this software without specific
     prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ORM_PTR_MULTIMAP_H
#define ORM_PTR_MULTIMAP_H
#pragma once

#include <boost/ptr_container/ptr_map_adapter.hpp>
#include <map>

namespace orm
{

template <class K, class T> class ptr_multimap
    : public boost::ptr_multimap_adapter<T, 
                                  std::multimap<K,void *>,
                                  boost::heap_clone_allocator>
{
    typedef boost::ptr_multimap_adapter<T,std::multimap<K,void*>,boost::heap_clone_allocator> base_type;
public:
    typedef typename base_type::const_iterator const_iterator;
    typedef typename base_type::iterator iterator;
    typedef typename base_type::const_reverse_iterator const_reverse_iterator;
    typedef typename base_type::reverse_iterator reverse_iterator;

    const_iterator cbegin() const { return const_iterator(this->base().cbegin()); }
    const_iterator cend() const { return const_iterator(this->base().cend()); }
    const_reverse_iterator crbegin() const { return const_reverse_iterator(cbegin()); }
    const_reverse_iterator crend() const { return const_reverse_iterator(cend()); }

    iterator insert(iterator it, const K &k, typename ptr_multimap::mapped_type x)/*{{{*/
    {
        this->enforce_null_policy(x, "Null pointer in ptr_multimap::insert()");
        typename ptr_multimap::auto_type ptr(x);
        typename base_type::ptr_iterator
            itret = this->base().insert(it.base(), std::make_pair(k,ptr.get()));
        ptr.release();
        return iterator(itret);
    }/*}}}*/
    iterator insert(iterator it, const K &k, std::auto_ptr<T> x)/*{{{*/
    {
        return insert(it, k, x.release());
    }/*}}}*/
};

template <class K, class T>
inline void swap(ptr_multimap<K,T> &m1, ptr_multimap<K,T> &m2)
{
    m1.swap(m2);
}

} // namespace orm

#endif

